﻿using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;

namespace LuDragon.Web
{  
    public class AutoFacBootStrapper
    { 

        public static void AutoFacInit()
        {
            var builder = new ContainerBuilder();

            //UI项目只用引用service和repository的接口，不用引用实现的dll。
            //如需加载实现的程序集，将dll拷贝到bin目录下即可，不用引用dll
            //根据名称约定（服务层的接口和实现均以Service结尾），实现服务接口和服务实现的依赖
            builder.RegisterAssemblyTypes(Assembly.Load("LuDragon.IService"), Assembly.Load("LuDragon.Service"))
                .AsImplementedInterfaces().PropertiesAutowired().InstancePerDependency();//.AsImplementedInterfaces();


            //注入所有Controller
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //注入所有apiController
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
    }
}